# -*- coding: utf-8 -*-
# flake8: noqa

from qiniu import Auth, put_file, etag
import qiniu.config
from qiniu import put_data
#需要填写你的 Access Key 和 Secret Key


access_key = 'v3n5FPnRvLujRroKe3X9FxjMSgtWCmMWBeharusu'
secret_key = 'd3uJUlaQ9iKh0b5XkhU-Tlx5hjrDiWDDeHA4xotw'

# 封装方法
def image_storage(image_data):
    #构建鉴权对象
    q = Auth(access_key, secret_key)

    #要上传的空间
    bucket_name = 'info17'

    #上传到七牛后保存的文件名
    key = None

    #生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)

    #要上传文件的本地路径
    # localfile = './sync/bbb.jpg'
    #
    # ret, info = put_file(token, key, localfile)
    # 上传二进制文件
    ret, info = put_data(token, key, image_data)
    print(info)
    print(ret)

    if info.status_code == 200:
        return ret.get("key")
    else:
        return ""
    # assert ret['key'] == key
    # assert ret['hash'] == etag(localfile)

# # 测试使用
# if __name__ == '__main__':
#
#     with open("./worm.jpg","rb") as f:
#         image_storage(f.read())